<?php
/**
 * @文件功能名称  : 角色管理
 * @Author      : yaoGrace
 * @Date        : 2021/11/30 23:36
 */
class roleController extends adminController{

    public function __init(){
        parent::__init();
        $this->roleModel = model("managerole");
        $this->menuModel = model("menu");
    }

    //展示角色页面
    public function index() {
       $this->getData = $this->roleModel->getRoleByPage();
    }

    //添加  角色
    public function add(){
        //获取所有非禁用的菜单
        $menu_list =  $this->menuModel->getMenuList();
        //整理为以mid为索引的数组
        $menuArr = [];
        foreach ($menu_list as $k  => $v ) {
            $menuArr[$v['mid']] = $v;
        }
        // 菜单按照等级划分出来 ,一维数组变为二维，三维甚至是多维数组
        $menu = $this->gettreeitems($menuArr);
        //需要将多维数组，转化为二维数组，方便选择
        $results = [];
        foreach ($menu as  $value) {
             $value['children'] =  isset($value['children'])?$this->formatMenus($value['children']):false;
             $results[] = $value;
        }
        //给前台传递
        $this->datas = $results;

        // 执行添加动作
        if(PG_POST){
            $data['role_name'] = $_POST['title'];
            $menus = is_array(isset($_POST['menu'])?$_POST['menu']:'');
            if(!$menus){$this->json("请选择权限对应的菜单","error");}
            // 将单选框 的key获取,并设置为json值
            $data['role_content'] = json_encode(array_keys($_POST['menu']));
            //添加
            $res = $this->roleModel->add($data);
            if($res){
                $this->json("保存成功");
            }else{
                $this->json("保存失败","error");
            }
        }
    }

    // 编辑 角色
    public function edit(){
        $role_id = $this->gets[0];
        $this->res = $this->roleModel->getFetchByID($role_id);
        $this->res['role_content'] = json_decode($this->res['role_content']);
        //获取所有非禁用的菜单
        $menu_list =  $this->menuModel->getMenuList();
        //整理为以mid为索引的数组
        $menuArr = [];
        foreach ($menu_list as $k  => $v ) {
            $menuArr[$v['mid']] = $v;
        }
        // 菜单按照等级划分出来 ,一维数组变为二维，三维甚至是多维数组
        $menu = $this->gettreeitems($menuArr);
        //需要将多维数组，转化为二维数组，方便选择
        $results = [];
        foreach ($menu as  $value) {
            $value['children'] =  isset($value['children'])?$this->formatMenus($value['children']):false;
            $results[] = $value;
        }
        //给前台传递
        $this->datas = $results;

        //  执行编辑操作
        if(PG_POST){
            $data['role_name'] = $_POST['title'];
            $menus = is_array(isset($_POST['menu'])?$_POST['menu']:'');
            if(!$menus){$this->json("请选择权限对应的菜单","error");}
            // 将单选框 的key获取,并设置为json值
            $data['role_content'] = json_encode(array_keys($_POST['menu']));
             $res = $this->roleModel->update($role_id,$data);
            if($res){
                $this->json("保存成功");
            }else{
                $this->json("保存失败","error");
            }
        }
    }
    /**
     * 菜单按照等级划分出来 ,一维数组变为二维，三维甚至是多维数组
     * @param $items
     * @return array
     */
    private function gettreeitems($items){
        $tree = [];
        foreach ($items as $item) {
            if(isset($items[$item['pid']])){
                $items[$item['pid']]['children'][] = &$items[$item['mid']];
            }else{
                $tree[] = &$items[$item['mid']];
            }
        }
        return $tree;
    }

    /**
     * 多维数组转化为二维数组
     * @param $items
     * @param array $res
     * @return array
     */
    private  function formatMenus($items,&$res = array()){
        foreach ($items as $item) {
            //如果不存在children 直接放数组
            if(!isset($item['children'])){
                $res[] = $item;
            }else{
                //  存在chiledren
                $temp = $item['children'];
                unset($item['children']);
                $res[] = $item;
                $this->formatMenus($temp,$res);
            }
        }
        return $res;
    }

    //删除角色
    public function del(){
        if(!PG_POST){return $this->json('非法操作', 'error');}
        $res=$this->roleModel->delete($_POST['id']);
        if($res){
            return $this->json('角色已删除');
        }else{
            return $this->json("删除失败","error");
        }
    }
}